package ch.zbw.pra.sysventory.presentation.panels;

import java.util.List;

import ch.zbw.pra.sysventory.application.controller.NicController;
import ch.zbw.pra.sysventory.model.Nic;

import com.vaadin.ui.Alignment;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;

/**
 * Painted das NicsArchivePanel
 * 
 * @author Ralph Koster
 */

public class NicsArchivePanel extends VerticalLayout {

	private static final long serialVersionUID = 1L;
	private NicController nicController;
	private Table nicsTable;
	
	public NicsArchivePanel(){
		this.nicsTable = new Table("NIC im Netzwerk:");
		this.nicController = new NicController();
		paint();
		loadNics();
	}
	
	private void paint() {
		this.setCaption("NIC");
		this.setStyleName("nicArchivePanel");
		nicsTable.setStyleName("table");
		nicsTable.addContainerProperty("NIC Name", String.class, null);
		nicsTable.addContainerProperty("NIC IPv4", String.class, null);
		nicsTable.addContainerProperty("NIC IPv6", String.class, null);
		nicsTable.addContainerProperty("MAC Adresse", String.class, null);
		nicsTable.addContainerProperty("NIC Standard Gateway", String.class, null);
		nicsTable.addContainerProperty("NIC DHCP", String.class, null);
		nicsTable.addContainerProperty("NIC Subnetz", String.class, null);
		this.addComponent(nicsTable);
		this.setComponentAlignment(nicsTable, Alignment.MIDDLE_CENTER);
		
	}
	
	private void loadNics() {
		nicsTable.removeAllItems();
		List<Nic> nics = nicController.loadNics();
		for (Nic nic : nics) {
			int nicId = nic.getNicId();
			String nicIp4 = nic.getIpv4Nic();
			String nicIp6 = nic.getIpv6Nic();
			String nicMac = nic.getMacAdressNic();
			String nicName = nic.getNameNic();
			String nicGateway = nic.getStandardGatewayNic();
			String nicDhcp = nic.getDhcpNic();
			String nicSubnet = nic.getSubnetNic();
			nicsTable.addItem(new Object[] {
					nicName,nicIp4,nicIp6,nicMac,nicGateway,nicDhcp,nicSubnet},nicId);
		}
	}

	public void updateNicArchivePanel() {
		loadNics();
	}
}
